import axios from 'axios'
import store from '@/store'
import router from '@/router'
import {X_ACCESS_TOKEN} from '../config/sysconsts'
import {setAccessToken, getLocalStorageCache, clearCacheLogin} from './cache'

/**
 *  添加授权信息
 * @param data
 */
export function addAuthToken (data) {
  setAccessToken(data)
}

/**
 *  刷新Token
 *
 * @returns {Promise<unknown>}
 */
export function refreshToken () {
  return new Promise((resolve, reject) => {
    resolve(true)
  })
}

/**
 *  移除授权
 *
 * @returns {Promise<unknown>}
 */
export function removeAuth () {
  // clearCacheLogin()
  // store.commit('user/SET_LOADASYNCROUTER_FLAG', false)
  // store.commit('user/SET_AUTHINFO', null)
  // store.commit('user/SET_USERINFO', null)
  // store.commit('permission/SET_ROUTERS', null)
  // delete axios.defaults.headers[X_ACCESS_TOKEN]
  return new Promise((resolve, reject) => {
    clearCacheLogin()
    store.commit('user/SET_LOADASYNCROUTER_FLAG', false)
    store.commit('user/SET_AUTHINFO', null)
    store.commit('user/SET_USERINFO', null)
    delete axios.defaults.headers[X_ACCESS_TOKEN]
    resolve(true)
  })
}

/**
 * 获取授权
 * @returns {*}
 */
export function getAuthToken () {
  const token = getLocalStorageCache(X_ACCESS_TOKEN)
  return token
}

export function checkAuthToken () {
  if (getAuthToken()) {
    return true
  }
  return false
}
export function userLogout (redirect) {
  store.dispatch('user/Logout').then(res => {
    if (redirect) {
      router.replace(`/login?redirect=` + redirect)
    } else {
      router.replace(`/login`)
    }
    // location.reload()
  })
}
// /**
//  * 获取登录用户信息
//  *
//  * @returns {any}
//  */
// export function getLoginUserAuth () {
//   return getLocalStorageCache(LOGIN_AUTH_INFO)
// }


